'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }


  //登录
  async login() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.query(`select * from login where username = "${username}" and password = "${password}"`)

    //遍历登录成功的数据存入到datas
    const datas = data.map(item => {
      return {
        username: item.username,
        url: item.url,
        name: item.name,
        shen: item.shen,
        id: item.id
      }
    })

    if (data.length) {
      //登陆成功传入datas
      ctx.body = {
        code: 1,
        datas,
        // shen:data[0].shen,
        token: jwt.sign({ ...data }, '1901A', { expiresIn: '12m' },),
      }
    } else {
      ctx.body = {
        code: 0,
      }
    }
  }

  // //注册
  // async register() {
  //   const { app, ctx } = this
  //   const { username, password } = ctx.request.body
  //   const data = await app.mysql.get('login', { username })

  //   if (!data && username !== '' && password !== '') {
  //     app.mysql.insert('login', { username, password })

  //     ctx.body = {
  //       code: 1,
  //       data
  //     }
  //   } else {
  //     ctx.body = {
  //       code: 0,
  //       data
  //     }
  //   }
  // }

  //渲染团购数据
  async Abulk() {
    const { app, ctx } = this
    const data = await app.mysql.select('group')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //渲染直播列表数据
  async Live() {
    const { app, ctx } = this
    const data = await app.mysql.select('live')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //筛选直播状态
  async liveState() {
    const { app, ctx } = this
    const { value1 } = ctx.request.body
    const data = await app.mysql.select("live", { where: { state: value1 } })
    console.log(value1);

    if (value1 === '请选择') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 0,
        data
      }
    }
  }

  //直播页面搜索
  async liveCheck() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from live where name like "%${value}%"`)

    ctx.body = {
      code: 1,
      data
    }
  }

  //渲染房源数据
  async housingList() {
    const { app, ctx } = this
    const data = await app.mysql.select('housing')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //房源管理添加数据
  async housingAdd() {
    const { app, ctx } = this
    const { name, need, price, time, sort } = ctx.request.body
    const data = await app.mysql.insert('housing', { name, need, price, time, sort })

    ctx.body = {
      code: 1,
      data
    }
  }

  //房源管理删除数据
  async housingDel() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('housing', { id })

    ctx.body = {
      code: 1,
      data
    }
  }

  //房源管理修改数据
  async housingEdit() {
    const { app, ctx } = this
    const { name, need, price, time, sort, id } = ctx.request.body
    const data = await app.mysql.update('housing', { id, name, need, price, time, sort })

    ctx.body = {
      code: 1,
      data
    }
  }

  //团购管理数据筛选
  async abulkState() {
    const { app, ctx } = this
    const { option } = ctx.request.body
    const data = await app.mysql.select("group", { where: { type: option } })

    if (option === '请选择') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 0,
        data
      }
    }
  }

  //团购管理数据搜索
  async abulkCheck() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    console.log(value);

    const data = await app.mysql.query(`select * from group where name like "%${value}%"`)
    // const data = await app.mysql.select("group", { where: { num: value } })
    // const data = await app.mysql.get('group',{ value })
    // console.log(value);
    // console.log(data);
    ctx.body = {
      code: 1,
      data
    }
  }

  async getCity() {
    const { ctx, app } = this;

    const { value } = ctx.request.body
    console.log(value.area.value);

    const data = await app.mysql.query(`select * from housing where need like "%${value.area.value}%"`)
    console.log(data);

    ctx.body = {
      code: "1",
      msg: '请求成功',
      data
    }
  }
}

module.exports = HomeController;
